package com.lemon.springboot.service.impl;

import com.lemon.springboot.dao.IMemoryDao;
import com.lemon.springboot.entity.CpuVo;
import com.lemon.springboot.entity.MemoryVo;
import com.lemon.springboot.service.IMemoryPageService;
import com.lemon.springboot.utils.DateUtil;
import com.lemon.springboot.utils.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author liuli
 * @Date 2020/12/21 9:04
 */
@Service
public class MemoryPageServiceImpl implements IMemoryPageService {

    @Autowired
    IMemoryDao memoryDao;

    @Override
    public Map<String, Object> findMemoryDataByDate(Date startTime, Date endTime, String token, int maxDataCount) {
        Map<String, Object> result =  new HashMap<>();
        List<MemoryVo> memoryVoList = memoryDao.findMemoryByDate(startTime, endTime, token);
        List<String> field = Arrays.asList(new String[maxDataCount]);
        List<String> data = null;
        for (int i = 0; i < maxDataCount; i = i+2) {
            if (i == 0) {
                field.set(i, DateUtil.dateFormat(new Date(startTime.getTime() + i * DateUtil._30SEC), DateUtil.parent));
            }else {
                field.set(i, DateUtil.dateFormat(new Date(startTime.getTime() + i * DateUtil._30SEC), "HH:mm"));
            }
            result.put("fields", field);
        }
        if (memoryVoList != null && memoryVoList.size() > 0) {
            data = Arrays.asList(new String[maxDataCount]);
            for (MemoryVo memoryVo : memoryVoList) {
                long time = memoryVo.getDate().getTime();
                int index = ((int) ((time - startTime.getTime()) / DateUtil._30SEC));
                data.set(index, NumberUtil.floatFormat(((double) memoryVo.getMemoryUse()) / 1024 / 1024, "0.00"));
            }
            result.put("data", data);
            result.put("model","内存大小" + NumberUtil.floatFormat(((double)memoryVoList.get(0).getMemoryTotal())/1024/1024, "0.00") + "GB");
        }
        return result;
    }
}
